/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.reddys.rad.inventory.utils;

import com.reddys.rad.inventory.db.Product;
import com.reddys.rad.inventory.db.PurchaseOrderDetails;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.EventHandler;
import javafx.scene.control.ComboBox;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TextField;
import javafx.scene.input.KeyEvent;
import javafx.scene.input.MouseEvent;

/**
 *
 * @author REDDYS
 */
public class EditingComboBox extends TableCell<Object, String>{
           private ComboBox textField ;
           private String columnName ;
            PurchaseOrderDetails purchaseOrderDetails ;
              ObservableList  items = FXCollections.observableArrayList();
        public EditingComboBox(ObservableList  items,String columnName) {
            this.items = items;
            this.columnName = columnName;
            textField = new ComboBox(items);
             
        }
 
        @Override
        public void startEdit() {
           
                super.startEdit();
                 createTextField();
                  setGraphic(textField);
               // textField.selectAll();

        }
 
        @Override
        public void cancelEdit() {
            super.cancelEdit();
            setText((String) getItem());
            setGraphic(null);
        }
 
        @Override
        public void updateItem(String item, boolean empty) {
            super.updateItem(item, empty);
            if (empty) {
                setText(null);
                setGraphic(null);
            } else {
                if (isEditing()) {
                    if (textField != null) {
                          textField.getEditor().setText(getString());
                    }
                    setText(null);
                    setGraphic(textField);
                } else {
                      setText(textField.getEditor().getText());
                    setGraphic(null);
                }
            }
        }
 
        private void createTextField() {
                purchaseOrderDetails = ((PurchaseOrderDetails)this.getTableRow().getItem());
              System.out.println(this.getTableColumn().getText());
               System.out.println(this.getTableView());
            new AutoCompleteComboBoxListener(textField,items);
            textField.setMinWidth(this.getWidth() - this.getGraphicTextGap()* 2);
            textField.getEditor().setOnKeyPressed(new EventHandler<KeyEvent>(){
         
                @Override
                public void handle(KeyEvent t) {
                    System.out.println("createtext"+textField.getEditor().getText());
                    if(columnName.equalsIgnoreCase("ProductCategory")){
                         purchaseOrderDetails.setProductCategory(textField.getEditor().getText());
                    }else if(columnName.equalsIgnoreCase("ProductID")){
                        //Product product  = new Product(textField.getEditor().getText());
                         purchaseOrderDetails.setProductId(textField.getEditor().getText());
                    }
                  
                }
                
            });
            textField.getEditor().setOnMouseExited(new EventHandler<MouseEvent>(){
         
                @Override
                public void handle(MouseEvent t) {
                       System.out.println("createtext"+textField.getEditor().getText());
                    if(columnName.equalsIgnoreCase("ProductCategory")){
                         purchaseOrderDetails.setProductCategory(textField.getEditor().getText());
                    }else if(columnName.equalsIgnoreCase("ProductID")){
                     //   Product product  = new Product(textField.getEditor().getText());
                         purchaseOrderDetails.setProductId(textField.getEditor().getText());
                    }
                  
                }
                
            });
            textField.getEditor().focusedProperty().addListener(new ChangeListener<Boolean>(){
                @Override
                public void changed(ObservableValue<? extends Boolean> arg0, 
                    Boolean arg1, Boolean arg2) {
                        if (!arg2) {
                            commitEdit(textField.getEditor().getText());
                        }
                }
            });
        }
 
        private String getString() {
            return getItem() == null ? "" : getItem().toString();
        }
 
}
